package com.algernoon.rpc.springrpc.rmi.spring;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import com.algernoon.rpc.springrpc.entity.User;

@Controller
public class SpringRmiController {

	Logger logger = LoggerFactory.getLogger(SpringRmiController.class);

	@Autowired
	@Qualifier("rmiService")
	private RmiProxyFactoryBean rmiService;

	private ISpringUserService userService;

	@PostConstruct
	public void init() {
		userService = (ISpringUserService) rmiService.getObject();
	}

	@GetMapping("/spring/rmi/user/{id}")
	@ResponseBody
	public User findUserById(@PathVariable Integer id) {
		logger.info("[findUserById] request param id is {}.", id);
		return userService.findUserById(id);
	}
}
